using Aurora.RabbitMq.Provider;
using Microsoft.Extensions.DependencyInjection;

namespace Aurora.RabbitMq;

/// <summary>
/// rabbitmq message factory 
/// </summary>
public class RabbitMqMessageConsumerFactory : IRabbitMqMessageConsumerFactory, IDisposable
{
    protected IServiceScope ServiceScope { get; }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="serviceScope"></param>
    public RabbitMqMessageConsumerFactory(IServiceScope serviceScope)
    {
        ServiceScope = serviceScope;
    }


    public IRabbitMqMessageConsumer Create(ExchangeDeclareConfiguration exchange, QueueDeclareConfiguration queue,
        string? connectionName = null)
    {
        var consumer = ServiceScope.ServiceProvider.GetRequiredService<RabbitMqMessageConsumer>();
        consumer.Initialize(exchange: exchange, queue: queue, connectionName: connectionName);
        return consumer;
    }

    public void Dispose()
    {
        ServiceScope?.Dispose();
    }
}